
package com.hlkj.pay.controller.admin;
/*
 * Hlpay-Plus aggregate payment system.
 * Copyright (c) 2024-2025 Hlpay Team Copyright has the right of final interpretation.
 */

import com.hlkj.pay.common.CommonResultCode;
import com.hlkj.pay.util.DateUtils;
import org.springframework.cache.CacheManager;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.hlkj.framework.common.pojo.CommonResult;
import com.hlkj.framework.common.pojo.PageResult;
import com.hlkj.pay.controller.BaseController;
import com.hlkj.pay.dto.admin.AdminUserBehaviorLogDto;
import com.hlkj.pay.dto.admin.QueryUserBehaviorLogDto;
import com.hlkj.pay.service.common.IBusinessLogService;
import com.hlkj.pay.vo.admin.req.log.UserBehaviorLogReq;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import java.util.Collection;

/**
 * @author HlpayTeam
 * @date 2024/11/04 17:11
 */
@Tag(name = "系统维护")
@Slf4j
@RequiredArgsConstructor
@RestController
@RequestMapping("/v1/system")
public class AdminSystemController extends BaseController {

    private final IBusinessLogService businessLogService;
    private final CacheManager cacheManager;

    @Operation(summary = "系统日志")
    @GetMapping("/log/behavior")
    public CommonResult<PageResult<AdminUserBehaviorLogDto>> queryBehaviorLogPage(UserBehaviorLogReq userBehaviorLogReq) {
        if (!StringUtils.hasText(userBehaviorLogReq.getStartTime()) || !StringUtils.hasText(userBehaviorLogReq.getEndTime())) {
            return CommonResult.error(CommonResultCode.ORDER_STATISTICS_TIME_NOT_EMPTY);
        }
        if (DateUtils.betweenDay(userBehaviorLogReq.getStartTime(), userBehaviorLogReq.getEndTime()) > 30) {
            String format = String.format(CommonResultCode.ORDER_DAY_STATISTICS_ERROR.getMsg(), "31");
            return CommonResult.error(CommonResultCode.ORDER_DAY_STATISTICS_ERROR.getCode(), format);
        }

        QueryUserBehaviorLogDto queryUserBehaviorLogDto = new QueryUserBehaviorLogDto();
        queryUserBehaviorLogDto.setUserId(userBehaviorLogReq.getUserId());
        queryUserBehaviorLogDto.setSysType(userBehaviorLogReq.getSysType());
        queryUserBehaviorLogDto.setLikeSn(userBehaviorLogReq.getSn());
        queryUserBehaviorLogDto.setLikeName(userBehaviorLogReq.getName());
        queryUserBehaviorLogDto.setLikeUserName(userBehaviorLogReq.getUserName());
        queryUserBehaviorLogDto.setEmail(userBehaviorLogReq.getEmail());
        queryUserBehaviorLogDto.setLikeModelName(userBehaviorLogReq.getModelName());
        processQueryDate(userBehaviorLogReq, queryUserBehaviorLogDto);
        queryUserBehaviorLogDto.setPageNo(userBehaviorLogReq.getPageNo());
        queryUserBehaviorLogDto.setPageSize(userBehaviorLogReq.getPageSize());
        PageResult<AdminUserBehaviorLogDto> pageResult = businessLogService.queryBehaviorList(queryUserBehaviorLogDto);
        return CommonResult.success(pageResult);
    }

    @Operation(summary = "清除系统缓存")
    @GetMapping("/cache")
    public CommonResult<Void> clearCache() {
        Collection<String> cacheNames = cacheManager.getCacheNames();
        cacheNames.stream().forEach(cacheName->{
            cacheManager.getCache(cacheName).clear();
        });
        log.info("系统缓存 清理完成");
        return CommonResult.success();
    }
}
